package com.ai.zhly.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ai.dmc.index.base.impl.base.BaseServiceImpl;
import com.ai.dmc.index.common.InitParam;
import com.ai.dmc.index.common.bean.LogBean;
import com.ai.dmc.index.common.bean.SmsBean;
import com.ai.dmc.index.common.bean.UserBean;
import com.ai.dmc.index.common.exception.BusinessException;
import com.ai.dmc.utils.json.JsonUtil;
import com.ai.frame.bean.InputObject;
import com.ai.frame.bean.OutputObject;
import com.ai.frame.logger.Logger;
import com.ai.frame.logger.LoggerFactory;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import net.sf.json.JSONObject;
/**
* @Title: LogServiceImpl.java
* @Description: 登录验证---接口
* @author xueyf3  
* @date 2016年5月23日 下午4:18:41
* Copyright：Copyright (c) 2016
* Company：北京亚信智慧数据科技有限公司
* @version V1.0
 */
public class LogServiceImpl extends BaseServiceImpl implements ILogService{
	// 定义对应Mapper文件的命名空间
    private static final String NAMESPACE = "Log.";
    
    public static Logger log = LoggerFactory.getServiceLog(LogServiceImpl.class);
   
    /**
     * 写入短信日志
     * 
     * @param inputObject
     * @param outputObject
     * @throws BusinessException
     */
	@Override
	public void insertSmsLog(SmsBean smsLog) throws BusinessException {
		
		try {
			this.getBaseDao().insert(NAMESPACE+"insertSmsLog", smsLog);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 查询日志列表
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void queryLogList(InputObject inputObject, OutputObject outputObject) throws BusinessException {

		Map<String, Object> resultMap = new HashMap<String, Object>();
		String curPage =inputObject.getParams().get( "curPage");
		String pageSize =inputObject.getParams().get( "pageSize");
		String keyword =inputObject.getParams().get( "keyword");
		inputObject.getParams().put("keyword",keyword);
		inputObject.getParams().put("pageSize",pageSize);
		inputObject.getParams().put("curPage",curPage);
		String queryDate = inputObject.getParams().get("queryDate");
		
		if (null == keyword) {
			keyword = "";
		}

		if (null == queryDate) {
			queryDate = "";
		}
		queryDate = queryDate.replace("/", "") + "%";
		inputObject.getParams().put("logTime", queryDate);
		if ("".equals(keyword)) {
			inputObject.getParams().put("searchKeyLike", keyword);
		} else {
			inputObject.getParams().put("searchKeyLike", "%" + keyword + "%");
		}
		int curPageNum = Integer.valueOf(curPage);
		int pageSizeNum = Integer.valueOf(pageSize);
		String orderBy = "logTime desc";
		
		//日志分页，如果是vertica数据库，特殊处理
		if ("vertica".equals(InitParam.DB_TYPE)) {
			int start = (Integer.valueOf(curPage)-1) * Integer.valueOf(pageSize) ;
			int end = Integer.valueOf(curPage) * Integer.valueOf(pageSize) ;
			inputObject.getParams().put("start", String.valueOf(start));
			inputObject.getParams().put("end", String.valueOf(end));
			String count = (String) this.getBaseDao().queryForObject(NAMESPACE+"queryLogListCount", inputObject.getParams());
			List<LogBean> logData = this.getBaseDao().queryForList(NAMESPACE+"queryLogListVertica", inputObject.getParams(),LogBean.class);
			Map<String, Object> map=new HashMap<String, Object>();
		    map.put("data",JsonUtil.Object2Json(logData));
		    map.put("pageSize", pageSize);
		    map.put("curPage", curPage);
		    map.put("totalPage", Integer.valueOf(count)/Integer.valueOf(pageSizeNum)+1);
		    map.put("totalNumber",  count);
		    JSONObject obj = JSONObject.fromObject(map);
			resultMap.put("result", null == obj ? "" : JsonUtil.Object2Json(obj));
	        outputObject.setObject(JsonUtil.Object2Json(resultMap));	
		}else {
			Page<Map<String, String>> page = (Page<Map<String, String>>) this.getBaseDao().queryForPage(NAMESPACE+"queryLogList", inputObject.getParams(), curPageNum, pageSizeNum, orderBy);
			Map<String, Object> map=new HashMap<String, Object>();
		    map.put("data",page.getResult());
		    map.put("pageSize", pageSize);
		    map.put("curPage", curPage);
		    map.put("totalPage", page.getPages());
		    map.put("totalNumber",  page.getTotal());
		    JSONObject obj = JSONObject.fromObject(map);
			resultMap.put("result", null == obj ? "" : JsonUtil.Object2Json(obj));
		    outputObject.setObject(JsonUtil.Object2Json(resultMap));
		}
		
	}

	

}
